package club.xinghe.servlet.teacher;

import club.xinghe.biz.TkBiz;
import club.xinghe.entity.Pdt;
import club.xinghe.entity.Tk;
import club.xinghe.entity.Xzt;
import club.xinghe.util.JsonServlet;
import club.xinghe.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/teacher/addEshowtkServlet")
public class addEshowtkServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        int pageNum = Integer.parseInt(request.getParameter("pageNum"));
        int ezid = Integer.parseInt(request.getParameter("zid"));
        int stid = Integer.parseInt(request.getParameter("code"));

        TkBiz tkBiz = new TkBiz();
        int count = tkBiz.tkCount(ezid,stid);//获得固定专业和固定题型的试题数量
        int total = PageUtil.getTotalPages(count, 15);//对试题进行分页的页数
        List<Tk> tkList = tkBiz.showUpEtk(pageNum,pageSize,ezid,stid);
        //选择题
        List<Xzt> xzdList = new ArrayList<>();
        //判断题
        List<Pdt> pddList = new ArrayList<>();

        for (int i = 0; i < tkList.size(); i++) {
            if (tkList.get(i).getStyle().getStyleId()==2){
                Pdt pdt = null;
                String[] u = tkList.get(i).gettContent().split("#");
                pdt = new Pdt(tkList.get(i).gettId(),u[0],tkList.get(i).gettAnswer());
                pddList.add(pdt);
            }
            if (tkList.get(i).getStyle().getStyleId()==1){
                Xzt xzt = null;
                String[] x = tkList.get(i).gettContent().split("#");
                xzt = new Xzt(tkList.get(i).gettId(),x[0],x[1],x[2],x[3],x[4],tkList.get(i).gettAnswer());
                xzdList.add(xzt);
            }
        }

        Map<String,Object> maps = new HashMap<>();
        maps.put("styleid",stid);
        maps.put("pageNum",pageNum);
        maps.put("total",total);
        if (stid==1){
            maps.put("List",xzdList);
        }
        if (stid==2){
            maps.put("List",pddList);
        }

        JsonServlet.getJson(request,response,maps);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
